package com.coolingme.web.pay.alipay;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.*;
import com.alipay.api.request.*;
import com.alipay.api.response.*;
import com.coolingme.properties.AlipayProperties;
import com.coolingme.properties.DefaultAlipayProperties;
import com.coolingme.util.LogUtil;

import java.io.UnsupportedEncodingException;

/**
 * 电脑网站支付工具类
 *
 * @author: 王越
 * @date: 2019-06-20 23:46:07
 */
public class AlipayUtil {

    /**
     * alipay.trade.query(统一收单线下交易查询)
     *
     * @param model            交易查询请求参数类
     * @param model.outTradeNo 订单支付时传入的商户订单号,和支付宝交易号不能同时为空。
     * @param model.tradeNo    支付宝交易号，和商户订单号不能同时为空 。优先取trade_no
     * @return JSONObject格式的查询结果
     * @throws AlipayApiException
     */
    public static JSONObject query(AlipayTradeQueryModel model) throws AlipayApiException {
        // 获得初始化的AlipayClient
        AlipayClient alipayClient = DefaultAlipayClientFactory.getAlipayClient();
        // 创建API对应的request
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        // 设置请求对象
        request.setBizModel(model);
        // 调用SDK，获取响应对象
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            System.out.println("统一收单线下交易查询调用成功");
        } else {
            System.out.println("统一收单线下交易查询调用失败");
        }
        String result = response.getBody();
        LogUtil.logResult("交易查询", result, DefaultAlipayProperties.LOG_PATH);
        JSONObject strj = JSON.parseObject(result).getJSONObject("alipay_trade_query_response");
        return strj;
    }

    /**
     * alipay.trade.close(统一收单交易关闭接口)
     *
     * @param model            交易关闭请求参数类
     * @param model.outTradeNo 订单支付时传入的商户订单号,和支付宝交易号不能同时为空。
     * @param model.tradeNo    支付宝交易号，和商户订单号不能同时为空 。优先取trade_no
     * @return JSONObject格式的查询结果
     * @throws AlipayApiException
     */
    public static JSONObject close(AlipayTradeCloseModel model) throws AlipayApiException {
        // 获得初始化的AlipayClient
        AlipayClient alipayClient = DefaultAlipayClientFactory.getAlipayClient();
        // 创建API对应的request
        AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
        // 设置请求对象
        request.setBizModel(model);
        // 调用SDK，获取响应对象
        AlipayTradeCloseResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            System.out.println("统一收单交易关闭接口调用成功");
        } else {
            System.out.println("统一收单交易关闭接口调用失败");
        }
        String result = response.getBody();
        LogUtil.logResult("交易关闭", result, DefaultAlipayProperties.LOG_PATH);
        JSONObject strj = JSON.parseObject(result).getJSONObject("alipay_trade_close_response");
        return strj;
    }

    /**
     * alipay.trade.refund(统一收单交易退款接口)
     *
     * @param model 退款请求参数类
     * @return 退款结果
     * @throws AlipayApiException
     */
    public static JSONObject refund(AlipayTradeRefundModel model) throws AlipayApiException {
        // 获得初始化的AlipayClient
        AlipayClient alipayClient = DefaultAlipayClientFactory.getAlipayClient();
        // 创建API对应的request
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();

        // 设置请求对象
        request.setBizModel(model);
//		// 同步通知地址：支付成功后页面自动跳转到传值地址中
//		request.setReturnUrl(AliWapPayConfig.RETURN_URL_WAP);
//		// 异步通知地址：支付成功生成之后会post返回订单信息详情到该地址，用于商家做业务逻辑处理
//		request.setNotifyUrl(AliWapPayConfig.NOTIFY_URL_WAP);
        // 调用SDK，获取响应对象
        AlipayTradeRefundResponse response = alipayClient.execute(request);
        // 返回结果
        String result = response.getBody();
        LogUtil.logResult("退款", result, DefaultAlipayProperties.LOG_PATH);
        JSONObject strj = JSON.parseObject(result).getJSONObject("alipay_trade_refund_response");
        return strj;
    }

    /**
     * alipay.trade.fastpay.refund.query(统一收单交易退款查询)
     *
     * @param model 退款查询请求参数类
     * @return 退款查询结果
     * @throws AlipayApiException
     */
    public static JSONObject refundQuery(AlipayTradeFastpayRefundQueryModel model) throws AlipayApiException {
        // 获得初始化的AlipayClient
        AlipayClient alipayClient = DefaultAlipayClientFactory.getAlipayClient();
        // 创建API对应的request
        AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
        // 设置请求对象
        request.setBizModel(model);
        // 调用SDK，获取响应对象
        AlipayTradeFastpayRefundQueryResponse response = alipayClient.execute(request);
        // 返回结果
        String result = response.getBody();
        LogUtil.logResult("退款查询", result, DefaultAlipayProperties.LOG_PATH);
        JSONObject strj = JSON.parseObject(result).getJSONObject("alipay_trade_fastpay_refund_query_response");
        return strj;
    }

    /**
     * alipay.data.dataservice.bill.downloadurl.query(查询对账单下载地址)
     *
     * @param model 查询对账单下载地址请求参数类
     * @return 退款查询结果
     * @throws AlipayApiException
     */
    public static JSONObject billQuery(AlipayDataDataserviceBillDownloadurlQueryModel model) throws AlipayApiException {
        // 获得初始化的AlipayClient
        AlipayClient alipayClient = DefaultAlipayClientFactory.getAlipayClient();
        // 创建API对应的request
        AlipayDataDataserviceBillDownloadurlQueryRequest request = new AlipayDataDataserviceBillDownloadurlQueryRequest();

//		model.setBillDate("2019-06");
//		model.setBillType("signcustomer");

        // 设置请求对象
        request.setBizModel(model);
        // 调用SDK，获取响应对象
        AlipayDataDataserviceBillDownloadurlQueryResponse response = alipayClient.execute(request);
        // 返回结果
        String result = response.getBody();
        LogUtil.logResult("查询对账单下载地址", result, DefaultAlipayProperties.LOG_PATH);
        JSONObject strj = JSON.parseObject(result)
                .getJSONObject("alipay_data_dataservice_bill_downloadurl_query_response");
        return strj;
    }

    /**
     * alipay.trade.page.pay(统一收单下单并支付页面接口)
     *
     * @param model 电脑网站支付请求参数类
     * @return from表单
     * @throws AlipayApiException           支付宝异常
     * @throws UnsupportedEncodingException URLEncoder转码异常
     */
    public static String pagePay(AlipayTradePagePayModel model)
            throws AlipayApiException, UnsupportedEncodingException {
        // 获得初始化的AlipayClient
        AlipayClient alipayClient = DefaultAlipayClientFactory.getAlipayClient();
        // 创建API对应的request
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        // 同步通知地址：支付成功后页面自动跳转到传值地址中
        request.setReturnUrl(AlipayProperties.RETURN_URL_PAGE);
        // 异步通知地址：支付成功生成之后会post返回订单信息详情到该地址，用于商家做业务逻辑处理
        request.setNotifyUrl(AlipayProperties.NOTIFY_URL_PAGE);
        // 设置请求参数对象
        request.setBizModel(model);

        // BizContent传值模式
        /*
         * String bizContent = "{\"out_trade_no\":\"" + modal.getOutTradeNo() + "\"," +
         * "\"total_amount\":\"" + modal.getTotalAmount() + "\"," + "\"subject\":\"" +
         * modal.getSubject() + "\"," + "\"body\":\"" + modal.getBody() + "\"," +
         * "\"product_code\":\"" + modal.getProductCode() + "\"}";
         * request.setBizContent(bizContent);
         */

        // 调用SDK，获取响应对象,post请求方式，返回from表单
        AlipayTradePagePayResponse response = alipayClient.pageExecute(request);

        // get请求方式，返回http链接。
//		AlipayTradePagePayResponse respose2 = alipayClient.pageExecute(request, "get");

        if (response.isSuccess()) {
            System.out.println("统一收单交易支付接口调用成功");
        } else {
            System.out.println("统一收单交易支付接口调用失败");
        }
        // 获取表单
        String form = response.getBody();
        LogUtil.logResult("电脑网站支付", form, DefaultAlipayProperties.LOG_PATH);
        return form;

    }

}